package com.tuo.controller;

import com.tuo.beans.Usr;
import com.tuo.service.UsrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/1/15.
 */
@Controller
public class UsrController {
    @Autowired
    UsrService usrService;

    @ResponseBody
    @RequestMapping("login")
    public Map<String, String> login(Usr usr, HttpSession session) {
        Map<String, String> map = new HashMap<String, String>();
        Usr loginUsr = usrService.login(usr);
        if (loginUsr != null) {
            map.put("message", "登陆成功");
            session.setAttribute("user", loginUsr.getName());
        } else {
            map.put("errorMsg", "用户名或者密码错误，登陆失败!");
        }
        return map;
    }

    @RequestMapping("logout")
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:index.htm";
    }
}
